/*
 * Copyright (C) 2009  Sergey Panasenko <sergey.panasenko@gmail.com>
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QSqlRelationalDelegate>
#include <QToolButton>
#include <QAction>
#include <QDebug>
#include <QString>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
 //   QToolButton clients_button = QToolButton(this);
    QAction* client_action = this->ui->mainToolBar->addAction("Clients");
    QAction* document_action = this->ui->mainToolBar->addAction("Documents");
    connect(client_action,SIGNAL(triggered()),this,SLOT(show_clients()));
    connect(document_action,SIGNAL(triggered()),this,SLOT(show_documents()));
    connect(ui->tableView,SIGNAL(clicked(QModelIndex)),this,SLOT(table_clicked(QModelIndex)));

    db = new Database(this);

    model_client = new QSqlRelationalTableModel();
    model_client->setTable("clients");
    model_client->removeColumn(0); // don't show the ID
    model_client->setHeaderData(0, Qt::Horizontal, tr("Name"));
    model_client->setHeaderData(1, Qt::Horizontal, tr("Type"));
    model_client->setHeaderData(2, Qt::Horizontal, tr("Phone"));
    model_client->setHeaderData(3, Qt::Horizontal, tr("Address"));
    model_client->setRelation(1, QSqlRelation("clients_type", "id", "type"));
    model_client->select();

    model_document = new QSqlRelationalTableModel();
    model_document->setTable("document");
//    model_document->removeColumn(0); // don't show the ID
    model_document->setHeaderData(1, Qt::Horizontal, tr("Type"));
    model_document->setHeaderData(2, Qt::Horizontal, tr("Number"));
    model_document->setHeaderData(3, Qt::Horizontal, tr("Client"));
    model_document->setHeaderData(4, Qt::Horizontal, tr("Data"));
    model_document->setHeaderData(5, Qt::Horizontal, tr("Payed"));
    model_document->setHeaderData(6, Qt::Horizontal, tr("Closed"));

    model_document->setRelation(1, QSqlRelation("document_type", "id", "type"));
    model_document->setRelation(3, QSqlRelation("clients", "id", "name"));
 //   model_document->setRelation(5, QSqlRelation("status", "id", "status"));
    model_document->select();




    ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
//    dialog = new DocumentDialog(this);



}
void MainWindow::show_clients()
{
    ui->tableView->setModel(model_client);
    ui->tableView->show();
    ui->tableView->resizeColumnsToContents();
    ui->tableView->setMinimumWidth(600);
            ui->tableView->setColumnWidth(1,300);
}
void MainWindow::show_documents()
{
    ui->tableView->setModel(model_document);
    ui->tableView->show();

    ui->tableView->resizeColumnsToContents();
    ui->tableView->setMinimumWidth(600);
//            ui->tableView->setColumnWidth(1,300);


}

void MainWindow::table_clicked(const QModelIndex & index)
{
    if (index.model()==model_document)
    {
        int id = QVariant(index.model()->data(index.sibling(index.row(),0))).toInt();
        qDebug() << id;
        //проверить, не открыт ли документ
        document = new Document(this);
        document->ID = id;
        db->getDoc(document);

        dialog = new  DocumentDialog(document,db,this);
        dialog->show();
    }


}

MainWindow::~MainWindow()
{
    delete ui;
}
